﻿$(function () {
    $('#table1').datagrid({
        loadMsg: "正在接收数据...",
        iconCls: 'icon-reload',
        url: '',
        idField: 'WarehouseID',
        height: document.documentElement.clientHeight - 150,
        striped: true,      //隔行变色
        singleSelect: true, //是否单选
        pagination: true,   //分页控件
        scrollbarSize: 0,
        rownumbers: true,   //行号
        onLoadSuccess: function (data) {
            $('#table1').datagrid("fillRows");
            $('#table1').datagrid('resize');
        },
        onClickRow: function (index, row) {
            $("#hWarehouseID").val(row.WarehouseID);
            PloadLocal();
        },
        loadFilter: function (data) {
            if (data.d) {
                return data.d;
            } else {
                return JSON.parse(data);
            }
        },
        onSortColumn: function (sort, order) {
            $("#hSort").val(sort);
            $("#hOrder").val(order);
            loadLocal();
            //使用后消失 在2个grid以上

            $('#table1').datagrid('resize');
        }
    });
    $(window).resize(function () {
        $('#table1').datagrid('resize');
    });

    $('#table2').datagrid({
        loadMsg: "正在接收数据...",
        iconCls: 'icon-reload',
        url: '',
        idField: 'StorageID',
        striped: true,      //隔行变色
        singleSelect: false, //是否单选
        pagination: true,   //分页控件
        scrollbarSize: 0,
        rownumbers: true,   //行号
        onLoadSuccess: function (data) {
            $('#table2').datagrid("fillRows");
            $('#table2').datagrid('resize');
        },
        onSortColumn: function (sort, order) {
            $("#hSort").val(sort);
            $("#hOrder").val(order);
            $('#table2').datagrid('resize');
        }
    });
    $(window).resize(function () {
        $('#table2').datagrid('resize');
    });

    //抽屉绑定
    Drawerlist();
})

//新增取消
function ItemClear() {
    $('#dd2').drawer({
        onCollapse: function () {
            // 抽屉关闭后的逻辑处理
            console.log('抽屉已关闭');
            // 添加您想要执行的操作
        }
    });
    $('#dd').drawer({
        onCollapse: function () {
            // 抽屉关闭后的逻辑处理
            console.log('抽屉已关闭');
            // 添加您想要执行的操作
        }
    });

    $('#dd').find('input.easyui-textbox').textbox('setValue', '');
    $('#dd2').find('input.easyui-textbox').textbox('setValue', '');
}

//抽屉绑定
function Drawerlist() {
    //绑定抽屉打开事件
    $('#dd').drawer({
        onExpand: function () {
        }
    });
    //绑定抽屉关闭事件
    $('#dd').drawer({
        onCollapse: function () {
            // 抽屉关闭后的逻辑处理
            console.log('抽屉已关闭');
            // 添加您想要执行的操作
        }
    });

    //绑定抽屉打开事件
    $('#dd2').drawer({
        onExpand: function () {
        }
    });
    //绑定抽屉关闭事件
    $('#dd2').drawer({
        onCollapse: function () {
            // 抽屉关闭后的逻辑处理
            console.log('抽屉已关闭');
            // 添加您想要执行的操作
        }
    });
}
//*****************************仓库事件***********************************
//查询
function loadLocal() {
    var WarehouseNO = $("#txtWarehouseNO").val();//编号
    var WarehouseName = $("#txtWarehouseName").val();//名称
    //排序字段
    var sort = $("#hSort").val();
    var order = $("#hOrder").val();
    $("#table1").datagrid({
        url: '/api/BAS/QueryBasWarehouseInfo',
        method: 'Get',
        queryParams: {
            WarehouseNO: WarehouseNO,
            WarehouseName: WarehouseName,
            sort: sort,
            order: order
        },
        loadFilter: function (data) {
            if (data.d) {
                return data.d;
            } else {
                return JSON.parse(data);
            }
        }
    });
    $("#hSort").val("");
    $("#hOrder").val("");
    $('#table1').datagrid('unselectAll');
}

//新增弹窗
function IndexOpen() {
    $('#dd').drawer('expand');
}

//删除
function Delet() {
    var row = $('#table1').datagrid('getSelected');
    var WarehouseID = row['WarehouseID'];
    if (WarehouseID == "") {
        Alert("请选择后再删除！");
        return false;
    }
    Confirm("确定要删除吗？", function () {
        var url = '/api/BAS/DeleteBasWarehouseInfo';
        let datalist = {
            WarehouseID: WarehouseID
        }
        DataService.PostJson(url, datalist, function (err, data) {
            if (err) {
                Notice(data);
            } else {
                Notice(data);
            }
            loadLocal();
        })
    });
}

//新增保存
function WarItemSave() {
    var url = '/api/BAS/SaveBasWarehouseInfo';
    var datalist = {
        WarehouseNO: $("#txtwtno").val(),
        WarehouseName: $("#txtwtname").val()
    }
    console.log(datalist);
    DataService.PostJson(url, datalist, function (err, res) {
        if (err) {
            Notice("保存成功!");
        } else {
            Notice("保存失败." + res);
        }
        loadLocal();
        ItemClear();
        $('#dd').find('input.easyui-textbox').textbox('setValue', '');
    });
}

//*****************************库位事件***********************************
//查询
function PloadLocal() {
    var WarehouseID = $("#hWarehouseID").val();
    var StorageNO = $("#txtstrNo").val();//编号
    var WarehouseName = $("#txtwhName").val();//名称
    //排序字段
    var sort = $("#hSort").val();
    var order = $("#hOrder").val();
    $("#table2").datagrid({
        url: '/api/BAS/QueryBasStorageInfo',
        method: 'Get',
        queryParams: {
            WarehouseID: WarehouseID,
            WarehouseName: WarehouseName,
            StorageNO: StorageNO,
            sort: sort,
            order: order
        },
        loadFilter: function (data) {
            if (data.d) {
                return data.d;
            } else {
                return JSON.parse(data);
            }
        }
    });
    $("#hSort").val("");
    $("#hOrder").val("");
    $('#table2').datagrid('unselectAll');
}

//新增弹窗
function PIndexOpen() {
    var selectedRows = $("#table1").datagrid('getSelections');
    if (selectedRows.length > 0) {
        // 有选中行
        $('#dd2').drawer('expand');
        $("#hWarehouseID").val(selectedRows[0].WarehouseID);
        $("#txtWhno").textbox('setValue', selectedRows[0].WarehouseName);
    } else {
        // 没有选中行
        Notice("请选中仓库添加库位.");
    }
}

//删除
function PDelet() {
    var row = $('#table2').datagrid('getSelected');
    var StorageID = row['StorageID'];
    if (StorageID == "") {
        Alert("请选择后再删除！");
        return false;
    }
    Confirm("确定要删除吗？", function () {
        var url = '/api/BAS/DeleteBasStorageInfo';
        let datalist = {
            StorageID: StorageID
        }
        DataService.PostJson(url, datalist, function (err, data) {
            if (err) {
                Notice(data);
            } else {
                Notice(data);
            }
            PloadLocal();
        })
    });
}

//新增保存
function StrItemSave() {
    var url = '/api/BAS/SaveBasStorageInfo';
    var datalist = {
        StorageNO: $("#txtStno").val(),
        WarehouseID: $("#hWarehouseID").val()
    }
    DataService.PostJson(url, datalist, function (err, res) {
        if (err) {
            Notice("保存成功!");
        } else {
            Notice("保存失败." + res);
        }
        PloadLocal();
        ItemClear();
        $('#dd2').find('input.easyui-textbox').textbox('setValue', '');
    });
}
//打印
function PrintCode() {
    var row = $('#table2').datagrid('getSelections');
    var ids = "";
    if (row.length > 0) {
        for (var i = 0; i < row.length; i++) {
            ids += "" + row[i]["StorageID"] + ",";
        }
    }
    var WarehouseID = $("#hWarehouseID").val();
    if (!WarehouseID) {
        Alert("请选择");
        return;
    }
    // 构建查询字符串
    var queryString = {
        WarehouseID: WarehouseID,
        CodeID: ids,
        FormID: 2
    }
    const params = encodeURIComponent(JSON.stringify(queryString))
    return OpenSubSubWindow("储位打印", "/BAS/UI/BasPrintBarCode.html?params=" + params, 650, 450,
        function () { })
}